jQuery(document).ready(function($) {
	$('body').append('<a href="#0" class="cd-top">Top</a>');
	
	// browser window scroll (in pixels) after which the "back to top" link is shown
	var offset = 300,
		offset_opacity = 1200,						// browser window scroll (in pixels) after which the "back to top" link opacity is reduced
		scroll_top_duration = 300,					// duration of the top scrolling animation (in ms)
		$back_to_top = $('.cd-top');				// grab the "back to top" link

	// hide or show the "back to top" link
	$(window).scroll(function(){
		( $(this).scrollTop() > offset ) ? $back_to_top.addClass('cd-is-visible') : $back_to_top.removeClass('cd-is-visible cd-fade-out');
		if( $(this).scrollTop() > offset_opacity ) { 
			$back_to_top.addClass('cd-fade-out');
		}
	});
	
	// smooth scroll to top
	$back_to_top.on('click', function(event){
		event.preventDefault();
		$('body,html').animate({
			scrollTop: 0 ,
		 	}, scroll_top_duration
		);
	});
});